package com.third.clock.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.third.clock.MainActivity;
import com.third.clock.R;
import com.third.clock.bean.MediaEntity;
import com.third.clock.manager.ImgaeEngineManager;
import com.third.clock.ui.view.HackyViewPager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2018/8/7 0007.
 */

public class ImageBrowserActivity extends Activity{

    private HackyViewPager mViewpager;

    private TextView mTextViewIndicator;

    private ImgaeEngineManager mImgaeEngine;

    private ImgaeBrowserAdapter mImgaeBrowserAdapter;

    private List<MediaEntity> mFileBeans;

    private int currentImagePosition=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browseimage);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initView(){

        mViewpager = (HackyViewPager) findViewById(R.id.bi_viewpager);
        mTextViewIndicator= (TextView) findViewById(R.id.textview_indicator);

    }

    public static void intentData(Context context,List<MediaEntity> list,int position){
        Intent intent = new Intent(context, ImageBrowserActivity.class);
        intent.putExtra("MediaEntitys", (Serializable) list);
        intent.putExtra("position", position);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    private void initData(){
        mImgaeEngine=new ImgaeEngineManager();
        mImgaeBrowserAdapter=new ImgaeBrowserAdapter();

        mFileBeans = (List<MediaEntity>) getIntent().getSerializableExtra("MediaEntitys");
        currentImagePosition=getIntent().getIntExtra("position",0);

        mViewpager.setVisibility(View.VISIBLE);
        mViewpager.setAdapter(mImgaeBrowserAdapter);
        mImgaeBrowserAdapter.setList(mFileBeans);
        mViewpager.setCurrentItem(currentImagePosition);
        mViewpager.setOnPageChangeListener(mOnPageChangeListener);
        if (mFileBeans!=null&&mFileBeans.size() != 0) {
            mTextViewIndicator.setText((currentImagePosition+1)+"/"+mFileBeans.size());
            mTextViewIndicator.setVisibility(View.VISIBLE);
        }
    }

    ViewPager.OnPageChangeListener mOnPageChangeListener=new ViewPager.OnPageChangeListener(){

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            currentImagePosition=position;

            if(mTextViewIndicator!=null){
                if(mFileBeans!=null&&mFileBeans.size()>currentImagePosition&&position!=0){
                    mTextViewIndicator.setText((currentImagePosition+1)+"/"+mFileBeans.size());
                    mTextViewIndicator.setVisibility(View.VISIBLE);
                }else if(position==0){
                    mTextViewIndicator.setText(1+"/"+mFileBeans.size());
                    mTextViewIndicator.setVisibility(View.VISIBLE);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private class ImgaeBrowserAdapter extends PagerAdapter {

        private View mCurrentView;

        private LayoutInflater layoutInflater;

        private List<MediaEntity> madapterList=new ArrayList<>();

        public ImgaeBrowserAdapter() {
            layoutInflater = LayoutInflater.from(ImageBrowserActivity.this);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            mCurrentView = (View) object;
        }

        public View getPrimaryItem() {
            return mCurrentView;
        }

        public void setList(List<MediaEntity> list){
            if(list!=null&&madapterList!=null){
                madapterList.clear();
                madapterList.addAll(list);
                notifyDataSetChanged();
            }
        }

        @Override
        public int getCount() {
            return madapterList==null?0:madapterList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public int getItemPosition(Object object) {
            // 最简单解决 notifyDataSetChanged() 页面不刷新问题的方法
            return POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            final PhotoView imageView = new PhotoView(ImageBrowserActivity.this);
            if(mFileBeans==null) return imageView;
            if(mFileBeans.size()<=position) return imageView;
            final String url = mFileBeans.get(position).getUrl();
            if(TextUtils.isEmpty(url)) return imageView;
            //图片加载
            mImgaeEngine.loadImage(ImageBrowserActivity.this, url, imageView);

            // Now just add PhotoView to ViewPager and return it
            imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View arg0, float arg1, float arg2) {
                     ImageBrowserActivity.this.finish();
                }
            });

            container.addView(imageView);
            return imageView;
        }
    }

}
